//定义注册模块
define(function (require, exports, modules) {
    let $ = require("jquery");
    require("jquery.validate");
    require("idcode");
    $.idcode.setCode();   //加载生成验证码方法
    //点击登录跳转到登录页面
    $(".title .login_txt").on("click", function (e) {
        window.location = "login.html";
    })
    // //验证用户名
    $.validator.addMethod("checkName", function (val, ele) {
        return /^[1]\d{10}$/.test(val) || /^\w+@\w+\.\w+$/.test(val);
    })
    //验证密码
    $.validator.addMethod("checkPwd", function (val, ele) {
        return /^[a-zA-Z0-9]{8,20}$/.test(val);
    })
    let validate = function () {
        $("form").validate({
            debug: true,//调试模式,即使验证成功也不会跳转到目标页面.
            messages: {
                uname: {
                    required: "请输入邮箱或手机号码",
                    checkName: "您输入的邮箱或手机号码不符合规范！",
                    remote: "该用户名已经存在",
                },
                upwd: {
                    required: "请输入8-20位字母或数字",
                    checkPwd: "密码请设为8-20位字母和数字！"
                },
                upwd2: {
                    required: "请再次输入密码",
                    equalTo: "两次输入的密码不一致",
                },
                securityCode: {
                    required: "请输入验证码",
                }
            },
            rules: {
                uname: {
                    required: true,
                    checkName: true,
                    remote: {
                        url: "../../server/user/checkeName.php",
                        type: "post",
                        dataType: "json",
                        data: {
                            uname: function () {
                                return $("[name=uname]").val();
                            },
                        }
                    }
                },
                upwd: {
                    required: true,
                    checkPwd: true,
                },
                upwd2: {
                    required: true,
                    equalTo: "#upwd",
                },
                securityCode: {
                    required: true,
                }
            },
            submitHandler: function () {
                let zymStatus = $.idcode.validateCode();
                let check = $("input[type=checkbox]")[0].checked;
                if (zymStatus && check) {
                    $.ajax({
                        url: "../../server/user/register.php",
                        type: "post",
                        data: $("form").serialize(),
                    }).then(function (res) {
                        if (res.status == 1) {
                             alert("注册成功");
                            window.location = "login.html";
                        }
                    })
                } else {
                      alert("注册失败");
                }
                return false;
            },
            errorPlacement: function (error, ele) {
                error.appendTo(ele.parent("p"));
                //存在一个bug.从验证正确再退回不正确状态,背景图片就不会显示了,考虑使用精灵图解决
                ele.siblings("label").css("background", "url(./imgs/login/l_060.jpg) no-repeat 2px");
            },
            success: function (err, ele) {
                $(ele).siblings("label").css("background", "");
            }
            //errorPlacement和success方式里的两个ele是不同的对象,一个是jq对象,一个是dom对象
        })
    }
    exports.validate = validate;//单个暴露
})
